#include "../src/CameraDriver/DaHeng/Driver.hpp"
#include "../src/CameraDriver/MindVision/Driver.hpp"
#include "../src/CameraDriver/Virtual/Driver.hpp"
#include "../src/ControlCollege.hpp"
#include "../src/FileFormat/DataIndex.hpp"
#include "../src/FileFormat/OnlyImageInfo.hpp"
#include "../src/FileFormat/OriginImg.hpp"
#include "../src/FileFormat/SingleImage.hpp"
#include "../src/ThriftServer.hpp"
#include <ClientPub.hpp>
#include <csignal>
#include <signal.h>
#include <spdlog/spdlog.h>
#include <thrift/server/TThreadedServer.h>
#include <unistd.h>

apache::thrift::server::TThreadedServer *server;

void SignalHandle(int sig) { server->stop(); }

int main() {
  spdlog::info(">>>>>> Control Booting");
  ControlCollege college;
  ClientPub broadcast(14003);

  college.AppendFileFormat<OriginImg>("OriginImg");
  college.AppendFileFormat<OnlyImageInfoFormat>("OnlyImageInfo");
  college.AppendFileFormat<DataIndex>("DataIndex");
  college.AppendFileFormat<SingleImage>("SingleImage");
  college.funcMsg = [&broadcast](std::string msg) { broadcast.Message(msg); };
  college.funcCameraOffline = [&broadcast](std::string uuid) {
    broadcast.CameraOffline(uuid);
  };
  college << (new DahengDriver());
  college << (new MindVisionDriver());
  college << (new VirtualDriver());

  server = new apache::thrift::server::TThreadedServer(
      std::make_shared<Thrift::NodeClientProcessorFactory>(
          std::make_shared<ThriftServerCloneFactory>(&college, &broadcast)),
      std::make_shared<apache::thrift::transport::TServerSocket>(14001),
      std::make_shared<apache::thrift::transport::TBufferedTransportFactory>(),
      std::make_shared<apache::thrift::protocol::TBinaryProtocolFactory>());

  college.Boot();
  signal(SIGINT, SignalHandle);
  signal(SIGTERM, SignalHandle);
  spdlog::info("<<<<<< Control Readying");
  server->serve();
  spdlog::info(">>>>>> Control Exiting");
  return 0;
}
